<!--
 * @Descripttion: 系统计划任务配置
 * @version: 1.2
 * @Author: sakuya
 * @Date: 2021年7月7日09:28:32
 * @LastEditors: sakuya
 * @LastEditTime: 2021年7月10日20:56:47
-->

<template>
	<el-dialog
		:title="titleMap[mode]"
		v-model="visible"
		:width="450"
		destroy-on-close
		@closed="$emit('closed')"
	>
		<el-form
			:model="form"
			:rules="rules"
			ref="dialogForm"
			label-width="150px"
			label-position="left"
		>
			<el-form-item label="任务名称" prop="taskName">
				<el-input
					v-model="form.taskName"
					placeholder="计划任务名称"
					clearable
				></el-input>
			</el-form-item>
			<el-form-item label="分组" prop="groupName">
				<el-input
					v-model="form.groupName"
					placeholder="计划任务分组"
					clearable
				></el-input>
			</el-form-item>
			<el-form-item label="定时规则" prop="interval">
				<sc-cron
					v-model="form.interval"
					placeholder="请输入Cron定时规则"
					clearable
					:shortcuts="shortcuts"
				></sc-cron>
			</el-form-item>
			<el-form-item label="定时规则描述" prop="intervalDescription">
				<el-input
					v-model="form.intervalDescription"
					placeholder="定时规则描述"
					clearable
				></el-input>
			</el-form-item>
			<el-form-item label="任务类型" prop="taskType">
				<el-select
					v-model="form.taskType"
					placeholder="请选择任务类型"
					@@change="taskTypeChange"
					clearable
				>
					<el-option label="DLL" :value="1"></el-option>
					<el-option label="API" :value="2"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item
				label="ApiUrl"
				prop="apiUrl"
				v-if="form.taskType == 2"
			>
				<el-input
					v-model="form.apiUrl"
					placeholder="请输入API地址"
					clearable
				></el-input>
			</el-form-item>
			<el-form-item
				label="API访问类型"
				prop="apiRequestType"
				v-if="form.taskType == 2"
			>
				<el-select
					v-model="form.apiRequestType"
					placeholder="请选择请求方式"
					clearable
				>
					<el-option label="GET" value="GET"></el-option>
					<el-option label="POST" value="POST"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item
				label="授权名"
				prop="apiAuthKey"
				v-if="form.taskType == 2"
			>
				<el-input
					v-model="form.apiAuthKey"
					placeholder="请输入API授权名"
					clearable
				></el-input>
			</el-form-item>
			<el-form-item
				label="授权值"
				prop="apiAuthValue"
				v-if="form.taskType == 2"
			>
				<el-input
					v-model="form.apiAuthValue"
					placeholder="请输入API授权值"
					clearable
				></el-input>
			</el-form-item>

			<el-form-item label="参数(API和DLL通用)" prop="apiParameter">
				<el-input
					v-model="form.apiParameter"
					placeholder="请输入调用参数"
					clearable
				></el-input>
			</el-form-item>

			<el-form-item
				label="DLL类型名"
				prop="dllClassName"
				v-if="form.taskType == 1"
			>
				<el-select
					v-model="form.dllClassName"
					placeholder="请选择DLL类型名"
					clearable
				>
					<el-option label="无" value=""></el-option>
					<el-option
						v-for="pclass in classJobs"
						:label="pclass"
						:value="pclass"
						:key="pclass"
					></el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="任务描述" prop="describe">
				<el-input
					type="textarea"
					:rows="5"
					v-model="form.describe"
					clearable
				></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
			<el-button type="primary" :loading="isSaveing" @click="submit()"
				>保 存</el-button
			>
		</template>
	</el-dialog>
</template>

<script>
import scCron from "@/components/scCron";

export default {
	components: {
		scCron,
	},
	emits: ["success", "closed"],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: "新增计划任务",
				edit: "编辑计划任务",
			},
			form: {
				taskName: "",
				groupName: "",
				interval: "",
				apiUrl: "",
				describe: "",
				// LastRunTime: "",
				Status: "",
				taskType: "",
				apiRequestType: "",
				apiAuthKey: "",
				apiAuthValue: "",
				apiParameter: "",
				dllClassName: "",
				dllActionName: "",
				id: 0,
				intervalDescription: "",
			},
			rules: {
				taskName: [
					{
						required: true,
						message: "请输入任务名称",
						trigger: "blur",
					},
				],
				groupName: [
					{
						required: true,
						message: "请输入分组名称",
						trigger: "blur",
					},
				],
				interval: [
					{
						required: true,
						message: "请输入任务时间间隔",
						trigger: "blur",
					},
				],
				apiUrl: [
					{
						required: true,
						message: "请输入调用的API地址",
						trigger: "blur",
					},
				],
				describe: [
					{
						required: true,
						message: "请输入任务描述",
						trigger: "blur",
					},
				],
				taskType: [
					{
						required: true,
						message: "请选择任务类型",
						trigger: "change",
					},
				],
				apiRequestType: [
					{
						required: true,
						message: "请选择API访问类型",
						trigger: "change",
					},
				],
				dllClassName: [
					{ required: true, message: "请输入类名", trigger: "blur" },
				],
			},
			visible: false,
			isSaveing: false,
			shortcuts: [
				{
					text: "每天8点和12点 (自定义追加)",
					value: "0 0 8,12 * * ?",
				},
			],

			classJobs: [],
		};
	},
	async mounted() {
		await this.getClassJob();
	},
	methods: {
		//显示
		open(mode = "add") {
			this.mode = mode;
			this.visible = true;
			return this;
		},
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res;
					if (this.form.id == null || this.form.id == "") {
						res = await this.$API.taskScheduler.add.post(this.form);
					} else {
						res = await this.$API.taskScheduler.update.post(
							this.form
						);
					}
					this.isSaveing = false;
					if (res.code == 200) {
						this.$emit("success");
						this.visible = false;
						this.$message.success("操作成功");
					} else {
						this.$alert(res.message, "提示", { type: "error" });
					}
				}
			});
		},
		//表单注入数据
		setData(data) {
			let deepCopiedObject = JSON.parse(JSON.stringify(data));
			this.form = deepCopiedObject;
		},
		taskTypeChange() {
			this.$refs["form"].clearValidate();
		},
		async getClassJob() {
			var res = await this.$API.taskScheduler.classJob.get();
			this.classJobs = res.data;
		},
	},
};
</script>

<style></style>
